package com.study.course.service.impl;

import java.util.Collections;
import java.util.List;

import com.study.common.core.domain.entity.SysDept;
import com.study.common.core.domain.entity.SysUser;
import com.study.common.utils.SecurityUtils;
import com.study.system.mapper.SysDeptMapper;
import com.study.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.study.course.mapper.SysCourseMapper;
import com.study.course.domain.SysCourse;
import com.study.course.service.ISysCourseService;

/**
 * 课程信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-21
 */
@Service
public class SysCourseServiceImpl implements ISysCourseService 
{
    @Autowired
    private SysCourseMapper sysCourseMapper;


    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询课程信息
     * 
     * @param courseId 课程信息主键
     * @return 课程信息
     */
    @Override
    public SysCourse selectSysCourseByCourseId(String courseId)
    {
        return sysCourseMapper.selectSysCourseByCourseId(courseId);
    }

    /**
     * 查询课程信息列表
     * 
     * @param sysCourse 课程信息
     * @return 课程信息
     */
    @Override
    public List<SysCourse> selectSysCourseList(SysCourse sysCourse) {
        // 获取当前用户ID
        Long currentUserId = SecurityUtils.getUserId();

        // 查询当前用户对象
        SysUser currentUser = sysUserMapper.selectUserById(currentUserId);
        if (currentUser == null) {
            return Collections.emptyList(); // 当前用户不存在
        }

        // 判断是否是超级管理员（系统账号）
        boolean isAdmin = SecurityUtils.isAdmin(currentUserId);

        if (!isAdmin && currentUser.getDeptId() != null) {
            // 非管理员，按部门筛选
            Long deptId = currentUser.getDeptId();
            SysDept dept = sysDeptMapper.selectDeptById(deptId);

            if (dept != null) {
                String deptName = dept.getDeptName(); // 获取部门名称
                sysCourse.setAcademy(deptName);      // 设置到查询条件中，假设 academy 存储的是 dept_name
            }
        }

        // 执行查询（已在 XML 中根据 academy 做了过滤）
        return sysCourseMapper.selectSysCourseList(sysCourse);
    }
    /**
     * 新增课程信息
     * 
     * @param sysCourse 课程信息
     * @return 结果
     */
    @Override
    public int insertSysCourse(SysCourse sysCourse)
    {
        return sysCourseMapper.insertSysCourse(sysCourse);
    }

    /**
     * 修改课程信息
     * 
     * @param sysCourse 课程信息
     * @return 结果
     */
    @Override
    public int updateSysCourse(SysCourse sysCourse)
    {
        return sysCourseMapper.updateSysCourse(sysCourse);
    }

    /**
     * 批量删除课程信息
     * 
     * @param courseIds 需要删除的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseByCourseIds(String[] courseIds)
    {
        return sysCourseMapper.deleteSysCourseByCourseIds(courseIds);
    }

    /**
     * 删除课程信息信息
     * 
     * @param courseId 课程信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseByCourseId(String courseId)
    {
        return sysCourseMapper.deleteSysCourseByCourseId(courseId);
    }

    @Override
    public List<SysUser> selectSysUserTeacher(Long roleId) {
        List<SysUser> userList = sysCourseMapper.selectSysUserTeacher(roleId);
        return userList;
    }
}
